package com.hzj.controller;

import com.hzj.pojo.Books;
import com.hzj.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    @RequestMapping("/getBooks")
    public String getBooks(Model model){
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("list",books);
        return "jsp/allBooks";
    }

    @RequestMapping("/toMain")
    public String toMain(){
        return "jsp/main";
    }

    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return  "jsp/addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.err.println("////////////////////////controller->addBook->"+books);
        bookService.addBook(books);
        return "redirect:getBooks";
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable int id,Model model){
        Books book = bookService.queryBookById(id);
        model.addAttribute("QBook",book);
        return "jsp/update";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        System.err.println("////////////////////////controller->updateBook->"+books);
        bookService.updateBook(books);
        return "redirect:getBooks";
    }

    @RequestMapping("/deleteBook/{id}")
    public String deleteBook(@PathVariable int id){
        bookService.deleteBookById(id);
        return "redirect:/book/getBooks";
    }

    @RequestMapping("/queryBookByName")
    public String queryBookByName(String bookName,Model model){
        List<Books> books = bookService.queryBookByName(bookName);
        model.addAttribute("list",books);
        return "jsp/allBooks";
    }
}
